package cn.edu.sgu.www.mhxysy.controller;

import cn.edu.sgu.www.mhxysy.consts.MimeType;
import cn.edu.sgu.www.mhxysy.entity.RoleModeling;
import cn.edu.sgu.www.mhxysy.pager.RoleModelingPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.RoleModelingService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/role_modeling", produces = MimeType.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleModelingController {

	private final RoleModelingService roleModelingService;

	@Autowired
	public RoleModelingController(RoleModelingService roleModelingService) {
		this.roleModelingService = roleModelingService;
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(RoleModeling modeling) {
		roleModelingService.insert(modeling);
		
		return JsonResult.success("添加成功");
	}

	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(RoleModeling modeling) {
		roleModelingService.updateById(modeling);

		return JsonResult.success("修改成功");
	}

	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<RoleModeling>> selectAll() {
		List<RoleModeling> list = roleModelingService.selectAll();

		return JsonResult.success(null, list);
	}

	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<RoleModeling> selectById(@RequestParam Integer id) {
		RoleModeling roleModeling = roleModelingService.selectById(id);

		return JsonResult.success(null, roleModeling);
	}

	@RequestMapping(value = "/selectImageById", method = RequestMethod.GET)
	public JsonResult<String> selectImageById(@RequestParam Integer id) {
		String image = roleModelingService.selectImageById(id);

		return JsonResult.success(null, image);
	}

	@RequestMapping(value = "/selectBySex", method = RequestMethod.GET)
	public JsonResult<List<RoleModeling>> selectBySex(@RequestParam(required = false) Integer sex) {
		List<RoleModeling> list = roleModelingService.selectBySex(sex);

		return JsonResult.success(null, list);
	}

	@RequestMapping(value = "/selectByPhyle", method = RequestMethod.GET)
	public JsonResult<List<RoleModeling>> selectByPhyle(
			@RequestParam(required = false) Integer phyle,
			@RequestParam(required = false) Integer gender) {
		List<RoleModeling> list = roleModelingService.selectByPhyle(phyle, gender);

		return JsonResult.success(null, list);
	}

	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<RoleModeling>> selectByPage(RoleModelingPager pager) {
		Page<RoleModeling> result = roleModelingService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public JsonResult<String> upload(MultipartFile file) throws IOException {
		String url = roleModelingService.upload(file);

		return JsonResult.success(null, url);
	}

}